/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IMAGE_KNIFE_C_KEKE_DOWN_SAMPLER_H
#define IMAGE_KNIFE_C_KEKE_DOWN_SAMPLER_H

#include "downsampling/down_sampling_at_most.h"
#include "imageknife_type.h"
#include "down_sampling_default.h"
#include "down_sampling_fit_center_memory.h"
#include "down_sampling_fit_center_quality.h"
#include "down_sampling_center_inside.h"
#include "down_sampling_at_least.h"
#include "log.h"
#include <multimedia/image_framework/image/image_common.h>
#include <unordered_map>

namespace ImageKnifePro {
class DownSampler {
public:
    static std::string GetDownSamplingInfo(Image_Size requestSize, DownSamplingStrategy strategy)
    {
        if (strategy == DownSamplingStrategy::NONE) {
            return std::string();
        }

        if (strategy == DownSamplingStrategy::DEFAULT) {
            return "DownSampling:Default";
        }

        return "DownSampling:" + std::to_string(static_cast<int>(strategy)) + "," + std::to_string(requestSize.width) +
               "," + std::to_string(requestSize.height);
    }

    static Image_Size CalculateSize(ImageFormat format, Image_Size sourceSize, Image_Size requestSize,
                                    DownSamplingStrategy strategy)
    {
        Image_Size imageSize = {.width = 0, .height = 0};
        IMAGE_KNIFE_CHECK_RETURN(sourceSize.width == 0, imageSize);
        IMAGE_KNIFE_CHECK_RETURN(sourceSize.height == 0, imageSize);

        DownSamplingBase *downSampling = GetDownSampling(strategy);
        if (downSampling == nullptr) {
            return imageSize;
        }

        if ((requestSize.width == 0 || requestSize.height == 0) && strategy != DownSamplingStrategy::DEFAULT) {
            // 默认策略不依赖测算宽高，但其余策略在宽高未获取的情况不进行降采样计算
            return imageSize;
        }

        float scaleFactor = downSampling->GetScaleFactor(sourceSize, requestSize);
        IMAGE_KNIFE_CHECK_RETURN(scaleFactor == 0, imageSize);
        if (format == ImageFormat::PNG) {
            imageSize.width = std::floor(sourceSize.width / scaleFactor);
            imageSize.height = std::floor(sourceSize.height / scaleFactor);
        } else if (format == ImageFormat::WEBP) {
            imageSize.width = std::round(sourceSize.width / scaleFactor);
            imageSize.height = std::round(sourceSize.height / scaleFactor);
        } else {
            imageSize.width = sourceSize.width / scaleFactor;
            imageSize.height = sourceSize.height / scaleFactor;
        }

        return imageSize;
    }
private:
    static DownSamplingBase *GetDownSampling(DownSamplingStrategy strategy)
    {
        static std::unordered_map<DownSamplingStrategy, DownSamplingBase *> downSamplerMap = {
            {DownSamplingStrategy::NONE, nullptr},
            {DownSamplingStrategy::DEFAULT, new DownSamplingDefault()},
            {DownSamplingStrategy::FIT_CENTER_MEMORY, new DownSamplingFitCenterMemory()},
            {DownSamplingStrategy::FIT_CENTER_QUALITY, new DownSamplingFitCenterQuality()},
            {DownSamplingStrategy::CENTER_INSIDE_MEMORY, new DownSamplingCenterInside(true)},
            {DownSamplingStrategy::CENTER_INSIDE_QUALITY, new DownSamplingCenterInside(false)},
            {DownSamplingStrategy::AT_LEAST, new DownSamplingAtLeast()},
            {DownSamplingStrategy::AT_MOST, new DownSamplingAtMost()}
        };

        return downSamplerMap[strategy];
    }
};
}

#endif //IMAGE_KNIFE_C_KEKE_DOWN_SAMPLER_H
